using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;
    private Rigidbody2D playerRigidbody;
    private Animator playerAnimator;
    private Joystick joystick;
    //public GameObject canvasObject;
    public float JumpSpeed;
    private BoxCollider2D feet;
    private bool isOnTheGround;
    private PolygonCollider2D attackArea;
    public LayerMask ground;
    float movingSign = Mathf.Epsilon;
    // Start is called before the first frame update
    void Start()
    {
        joystick = GameObject.FindGameObjectWithTag("SteeringWheel").GetComponent<Joystick>();
        playerRigidbody = GetComponent<Rigidbody2D>();
        playerAnimator = GetComponent<Animator>();
        feet = GetComponent<BoxCollider2D>();
        attackArea = this.transform.GetComponentInChildren<PolygonCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        Run();
        CheckGround();
        SwichAnimation();
    }

    void Flip(bool flip)
    {
        if (flip)
        {
            transform.localRotation = Quaternion.Euler(0, 180, 0);
        }
        else
        {
            transform.localRotation = Quaternion.Euler(0, 0, 0);
        }
    }
    void Run()
    {
        float moveDir = joystick.Horizontal;// -1 ~ 1
        float palyerVectorX = playerRigidbody.velocity.x;
        float vecX = isOnTheGround ? (moveDir > movingSign ? 1 : moveDir < movingSign * -1 ? -1 : 0) * speed :
            (palyerVectorX > movingSign ? 1 : palyerVectorX < movingSign * -1 ? -1 : 0) * speed;
        bool needFlip = moveDir < movingSign;
        bool isRunning = Mathf.Abs(moveDir) > 0.5f;
        bool needSlowDown = !isOnTheGround && ((needFlip && playerRigidbody.velocity.x > movingSign) || !needFlip && playerRigidbody.velocity.x < movingSign * -1);
        bool isAttacking = playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Attack");
        Vector2 playerVel = new Vector2(vecX, playerRigidbody.velocity.y);
        if (needSlowDown)
        {
            playerVel.x += playerVel.x * -1f * 0.1f;

        }
        if ((isOnTheGround && (isAttacking || !isRunning)))
        {
            playerVel.x = 0;
        }

        playerRigidbody.velocity = playerVel;

        if (isRunning)
        {
            Flip(needFlip);
        }
    }

    public void Jump()
    {
        if (isOnTheGround)
        {
            playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, JumpSpeed);
        }
    }
    void CheckGround()
    {
        isOnTheGround = feet.IsTouchingLayers(LayerMask.GetMask("Ground"));
        playerAnimator.SetBool("Falling", !isOnTheGround);
    }

    void SwichAnimation()
    {
        bool isAttacking = playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Attack");
        if (isOnTheGround)
        {
            playerAnimator.SetBool("Running", playerRigidbody.velocity.x != 0);
            playerAnimator.SetBool("Idel", playerRigidbody.velocity.x == 0);
            playerAnimator.SetBool("Jumpping", false);
        }
        else if (!isOnTheGround)
        {
            playerAnimator.SetBool("Running", false);
            playerAnimator.SetBool("Jumpping", playerRigidbody.velocity.y > 0);
            playerAnimator.SetBool("Falling", playerRigidbody.velocity.y <= 0);
        }
    }
    void ShowAttackArea()
    {
        attackArea.enabled = true;
    }
    void HideAttackArea()
    {
        attackArea.enabled = false;
    }
}
